package gpl.scotlandyard.fugitive.hint.home;

import gpl.scotlandyard.beans.basics.Player;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

class PlayersListAdapter extends BaseAdapter {

  private final List<Player> players;
  private final int resource;
  private final Context context;

  public PlayersListAdapter(Context context, int resource, List<Player> playersList) {
    this.resource = resource;
    this.players = playersList;
    this.context = context;
  }

  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    return PlayerViewFactory.create(context, resource, parent, getItem(position));
  }

  @Override
  public int getCount() {
    return players.size();
  }

  public List<Player> getPlayers() {
    return players;
  }

  public List<Player> getAvailablePlayers() {
    List<Player> availablePlayers = new LinkedList<Player>();
    for (Player player : players) {
      if (hasName(player) && hasPosition(player)) {
        availablePlayers.add(player);
      }
    }
    return availablePlayers;
  }

  private boolean hasPosition(Player player) {
    return player.getNode() != null;
  }

  private boolean hasName(Player player) {
    return player.getName() != null && !player.getName().isEmpty();
  }

  @Override
  public Player getItem(int position) {
    return players.get(position);
  }

  @Override
  public long getItemId(int position) {
    return players.get(position).hashCode();
  }

}
